###--file: ebm.tcl
#    date: 2021.03.07
#    code: gbk
###
package require Tk
package require rl_http
package require rl_json
namespace path {::rl_json}



# 向后端发送json，返回值是http_response的body
proc send_json {method url json} {

    rl_http instvar h $method $url -headers {Content-Type application/json} -data [encoding convertto utf-8 $json]

    puts "json is :$json"

    set res_body  [encoding convertfrom utf-8 [$h body]]

    return $res_body
}

# 图形界面初始化，包含一些全局变量以及组件的设置、布局
proc init {} {
    set ::lists {}
    set ::url http://127.0.0.1:3000/api/person/name
    set ::cols {serialnum name id sex company}
    set ::col_names {SerialNum Name Id Sex Company} 
     
    
    ttk::labelframe .lf_param -text "Parameter:" -width 50 -height 10
    label .lf_param.label -text {name: }
    ttk::labelframe .lf_result -text "Results:" -width 50  -height 10
    ttk::entry .lf_param.entry -textvariable name_param

    ttk::button .lf_param.button -text " go " -command {submit $name_param $url}
    
    bind .lf_param.entry <Return> {
       submit $name_param $url
    }
    grid .lf_param.label .lf_param.entry .lf_param.button    
    ttk::treeview .tree -columns "$::cols" -show headings \
        -yscroll ".vsb set" -xscroll ".hsb set"
    ttk::scrollbar .vsb -orient vertical -command ".tree yview"
    ttk::scrollbar .hsb -orient horizontal -command ".tree xview"
    
    pack .lf_param
    pack .lf_result -fill both -expand 1

    set_treeview
    #pack .lf_result -side top -fill x

    #listbox .lf_result.listbox -width 20 -height 10 \
        -relief sunken \
        -width 50 -height 16 -setgrid 1

    #pack .lf_result.listbox  -side top -fill x
}

# 设置treeview抬头列
proc set_treeview {} {

    image create photo upArrow -data {
        R0lGODlhDgAOAJEAANnZ2YCAgPz8/P///yH5BAEAAAAALAAAAAAOAA4AAAImhI+
        py+1LIsJHiBAh+BgmiEAJQITgW6DgUQIAECH4JN8IPqYuNxUAOw==}

    image create photo downArrow -data {
        R0lGODlhDgAOAJEAANnZ2YCAgPz8/P///yH5BAEAAAAALAAAAAAOAA4AAAInhI+
        py+1I4ocQ/IgDEYIPgYJICUCE4F+YIBolEoKPEJKZmVJK6ZACADs=}

    image create photo noArrow -height 14 -width 14
    set font [ttk::style lookup Heading -font]
    foreach col "$::cols" name "$::col_names" {
        .tree heading $col -text $name -image noArrow -anchor w 

        .tree column $col -width [expr {
            [font measure $font $name] + [image width noArrow] + 5
        }]
    }
    grid .tree .vsb -in .lf_result -sticky nsew
    grid .hsb       -in .lf_result -sticky nsew
    grid column .lf_result 0 -weight 1
    grid row    .lf_result 0 -weight 1

}

# 按下按钮后提交参数
proc submit {param  url} {
    set wi {.vsb}
    set type {ttk::scrollbar}
    set opt {-orient vertical -command ".tree yview"}
    refresh $wi $type $opt

    set wi {.hsb}
    set type {ttk::scrollbar}
    set opt {-orient vertical -command ".tree xview"}
    refresh $wi $type $opt
    
    set wi {.tree}
    set type {ttk::treeview}
    set opt {-columns "$::cols" -show headings -yscroll ".vsb set" -xscroll ".hsb set" }
    #set proc_lay {grid .tree .vsb -in .lf_result -sticky nsew}
    refresh $wi $type $opt;#$proc_lay



    set_treeview


    
    set json_body [format {{"name": "%s"}} $param]

    set body [send_json POST $url $json_body]

    set font [ttk::style lookup Treeview -font]

    json foreach item $body {
        set id [json get $item id]
        set name  [json get $item name]
        set sex [json get $item sex]
        set company [json get $item company_id]
        set sn [json get $item sn]
            
    
        .tree insert {} end -values [list $sn $name $id $sex $company]
        
        # comment begin
        if {0} {
            foreach col "$::cols" {
                set len [font measure $font "[set $col]  "]
            
                if {[.tree column $col -width] < $len} {
                    .tree column $col -width $len
                }
            }
        }
        ## comment end
    }
        
}


# 刷新部件，带类型、参数
proc refresh {widget type options} {
    catch {
        destroy $widget
        eval $type $widget $options
        #eval $layout
        #pack $widget  -side top -fill x
    }
}

init;# 执行界面初始化，程序启动
